package online.heycm.rbac.controller.user;

import online.heycm.platform.common.entity.result.Optional;
import online.heycm.platform.common.entity.result.PageResult;
import online.heycm.platform.common.entity.result.Result;
import online.heycm.rbac.common.constant.ErrorCode;
import online.heycm.rbac.common.utils.AesUtil;
import online.heycm.rbac.common.utils.RegUtil;
import online.heycm.rbac.param.user.UserAddReq;
import online.heycm.rbac.param.user.UserDetailRes;
import online.heycm.rbac.param.user.UserEditReq;
import online.heycm.rbac.param.user.UserPageReq;
import online.heycm.rbac.param.user.UserPageRes;
import online.heycm.rbac.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理
 *
 * @author hey
 * @version 1.0
 * @date 2024/1/19 14:15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<UserPageRes>> pageUser(UserPageReq req) {
        Optional<PageResult<UserPageRes>> optional = userService.pageUser(req);
        return Result.optional(optional);
    }

    /**
     * 获取用户详情
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/detail/{userId}")
    public Result<UserDetailRes> getUserDetail(@PathVariable int userId) {
        Optional<UserDetailRes> optional = userService.getUserDetail(userId);
        return Result.optional(optional);
    }

    /**
     * 新增用户
     *
     * @param req
     * @return
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody @Validated UserAddReq req) {
        // 手机号校验、加密
        if (!RegUtil.isPhone(req.getPhone())) {
            return Result.error(ErrorCode.USER_PHONE);
        }
        req.setPhone(AesUtil.encrypt(req.getPhone()));
        // 密码强度校验
        if (!RegUtil.isPassword(req.getAuthCredential())) {
            return Result.error(ErrorCode.USER_PASSWORD);
        }
        // 身份证校验、加密
        if (StringUtils.hasText(req.getIdCard())) {
            if (!RegUtil.isIdCard(req.getIdCard())) {
                return Result.error(ErrorCode.USER_ID_CARD);
            }
            req.setIdCard(AesUtil.encrypt(req.getIdCard()));
        }
        return Result.optional(userService.addUser(req));
    }

    /**
     * 修改用户
     *
     * @param req
     * @return
     */
    @PostMapping("/edit")
    public Result editUser(@RequestBody @Validated UserEditReq req) {
        // 如果有手机号，则校验
        if (StringUtils.hasText(req.getPhone())) {
            if (!RegUtil.isPhone(req.getPhone())) {
                return Result.error(ErrorCode.USER_PHONE);
            }
            req.setPhone(AesUtil.encrypt(req.getPhone()));
        }
        // 如果有身份证，则校验
        if (StringUtils.hasText(req.getIdCard())) {
            if (!RegUtil.isIdCard(req.getIdCard())) {
                return Result.error(ErrorCode.USER_ID_CARD);
            }
            req.setIdCard(AesUtil.encrypt(req.getIdCard()));
        }
        // 如果有密码，则校验
        if (StringUtils.hasText(req.getAuthCredential())) {
            if (!RegUtil.isPassword(req.getAuthCredential())) {
                return Result.error(ErrorCode.USER_PASSWORD);
            }
        }
        return Result.optional(userService.editUser(req));
    }

}
